#include "../2_source/server.hpp"



// 管理所有的连接
unordered_map<uint64_t, PtrConnection> _conns;
uint64_t connid = 0;   
EventLoop base_loop;
LoopThreadPool* loop_pool;

void ConnectionDestroy(const PtrConnection& conn)
{
    _conns.erase(conn->Id());
}
const void OnConnected(const PtrConnection& conn)
{
    DBG_LOG("new connection:%p", conn.get());
}
void OnMessage(const PtrConnection& conn, Buffer* buf)
{ 
    DBG_LOG("%s", buf->ReadPosition());
    buf->MoveReadOffset(buf->ReadableSize());
    string str = "hello,world~!";
    conn->Send(str.c_str(), str.size());
    conn->ShutDown();
}
void NewConnection(int fd)
{
    connid++;
    PtrConnection conn(new Connection(loop_pool->NextLoop(), connid, fd));
    conn->SetConnectedCallback(bind(OnConnected, placeholders::_1));
    conn->SetMessageCallback(bind(OnMessage, placeholders::_1, placeholders::_2));
    // conn->SetClosedCallback();
    conn->SetServerClosedCallback(bind(ConnectionDestroy, placeholders::_1));

    conn->EnableInactiveRelease(10);    // 启动非活跃超时销毁
    conn->Established();                // 就绪初始化 
    _conns.insert(make_pair(connid, conn));
    DBG_LOG("new ----");
}

int main()
{
    loop_pool = new LoopThreadPool(&base_loop);
    loop_pool->SetThread_count(2);
    loop_pool->CreateThreadPoll();

    Acceptor acceptor(&base_loop, 8080);
    acceptor.SetAcceptorCallback(bind(NewConnection,placeholders::_1));
    acceptor.StartListen();

    base_loop.start();
    

    return 0;
}






// ----------------------------------------------------------------------------------------------


// int main()
// {
//     srand(time(NULL));
//     EventLoop loop;
//     Socket lis_sock;
//     lis_sock.CreateServer(8080);
//     // 为监听套接字，创建一个channel进行事件的管理，以及事件的处理
//     Channel channel(&loop, lis_sock.Fd());
//     // 获取新链接，为新链接创建channel并添加监控
//     channel.SetReadCallback(bind(Acceptor,&loop,&channel));
//     channel.EnableRead();   //启动事件监控
//     while(1)
//     {
//         loop.start();
//     }
//     lis_sock.Close();


//     return 0;
// }

// void HandleClose(Channel* channel)
// {
//     DBG_LOG("close: %d", channel->Fd());
//     channel->Remove(); //移除监控
//     delete channel;
// }

// void HandleRead(Channel* channel)
// {

//     int fd = channel->Fd();
//     char buff[1024] = {0};
//     int ret = recv(fd,buff,1023,0);
//     if( ret <= 0 )
//     {
//         return HandleClose(channel);    //关闭释放
//     }
//     DBG_LOG("%s", buff);
//     channel->Enablewrite();     //启动可写事件
// }
// void HandleWrite(Channel* channel)
// {

//     int fd = channel->Fd();
//     const char* buff = "好兄弟，在吗？";
//     int ret = send(fd,buff, strlen(buff),0);
//     if(ret < 0)
//     {
//         return HandleClose(channel);    //关闭释放
//     }
//     channel->DisableWrite();    //关闭写监控
// }
// void HandleError(Channel* channel)
// {
//     return HandleClose(channel);    //关闭释放 
// }
// void HandleEvent(EventLoop* loop, Channel* channel, uint64_t timerid)
// {
//     loop->TimerRefresh(timerid);
// }

// void Acceptor(EventLoop* loop, Channel* lst_channel)
// {
//     int fd = lst_channel->Fd();
//     int newfd = accept(fd,nullptr,nullptr);
//     if(newfd < 0) return ;

//     uint64_t timerid = rand() % 10000;
//     Channel* channel = new Channel(loop, newfd);
//     channel->SetReadCallback(bind(HandleRead, channel));
//     channel->SetWriteCallback(bind(HandleWrite, channel));
//     channel->SetCloseCallback(bind(HandleClose, channel));
//     channel->SetErrorCallback(bind(HandleError, channel));
//     channel->SetEventCallback(bind(HandleEvent, loop,channel,timerid));
//     // 非活跃连接的超时释放，10秒后关闭连接
//     // 注意：定时销毁任务，必须在启动读事件之前，因为有可能启动了事件监控后，立即就有了事件，但是这时候还没有任务
//     loop->TimerAdd(timerid, 10, bind(HandleClose, channel));
//     channel->EnableRead();
// }